home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / N-P / NIFTY / myCShell / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-17  |  12.4 KB  |  641 lines  |  [TEXT/KAHL]

  1. /*********************************************************
  2.  "misc.c"
  3.  
  4.  by John A. Love, III [ Washington Apple Pi Users' Group]
  5.  
  6.  using Symantec's "THINK C", v 5.00
  7.  *********************************************************/
  8.  
  9.  
  10. #ifndef __SOUND__
  11. #include <Sound.h>
  12. #endif
  13.  
  14. #ifndef _H_math
  15. #include <math.h>
  16. #endif
  17.  
  18. #ifndef    __TRAPS__
  19. #include <Traps.h>
  20. #endif
  21.  
  22. #include <CType.h>
  23.  
  24. #include "protos"
  25.  
  26. #include "globals.h"
  27. #include "extern.h"
  28.  
  29. #include "floatingWindow.h"
  30.  
  31.  
  32. void            FatalSystemCrash (void);
  33. void            MyMoreMasters (short numMasterPtrs);
  34. TrapType        GetTrapType (short theTrap);
  35. short            GetTrapNum (short theTrap);
  36. short            NumToolboxTraps (void);
  37.  
  38.  
  39.         short        SysEnvironsVersion = 1;
  40.         SysEnvRec    theWorld;
  41.  
  42.  
  43.  
  44.  
  45. /* ====================================
  46.    No further explanation is required :
  47.    ==================================== */
  48.  
  49.  void FatalSystemCrash (void)    {
  50.  
  51.     ExitToShell();
  52.  
  53.  }    /* FatalSystemCrash */
  54.  
  55.  
  56.  
  57.  void MyMoreMasters (short numMasterPtrs)    {
  58. /* See Technical Note #53: */
  59.  
  60.         short    oldMoreMast;
  61.         THz        zone;
  62.  
  63.  
  64.     zone = GetZone();
  65.  
  66.     oldMoreMast = zone->moreMast;
  67.     zone->moreMast = numMasterPtrs;
  68.     MoreMasters();                    /* Calls itself "moreMast" times. */
  69.     zone->moreMast = oldMoreMast;
  70.     if (MemError()) ExitToShell();
  71.  
  72.  }    /* MyMoreMasters */
  73.  
  74.  
  75.  
  76. void InitManagers (void)    {
  77.  
  78.     MaxApplZone();
  79.     MyMoreMasters(15);
  80.     InitGraf(&thePort);
  81.     InitFonts();
  82.     InitWindows();
  83.     InitMenus();
  84.     TEInit();
  85.     InitDialogs(&FatalSystemCrash);
  86.     ;
  87.     FlushEvents(everyEvent, 0);
  88.     InitCursor();
  89.  
  90. }    /* InitManagers */
  91.  
  92.  
  93.  
  94. /* ================================================================================
  95.    Test for the presence of a Mac with Color QuickDraw and a Color Monitor that the
  96.    user has set to Color via the Control Panel or using the "Switch-A-Roo" FKEY.
  97.  
  98.    Return the color depth:
  99.    ================================================================================ */
  100.  
  101. Boolean TestForColor (short *pixelDepth)    {
  102.  
  103.         OSErr        whoCares;    /* Compiler's "glue" for _SysEnvirons fills-in
  104.                                    all fields EXCEPT systemVersion.            */
  105.  
  106.     whoCares = SysEnvirons(SysEnvironsVersion, &theWorld);
  107.     if (theWorld.hasColorQD)
  108.     {
  109.         *pixelDepth = (**((*GetGDevice())->gdPMap)).pixelSize;
  110.         return(true);
  111.     }
  112.     else
  113.     {
  114.         *pixelDepth = 1;
  115.         return(false);
  116.     };
  117.  
  118. }    /* TestForColor */
  119.  
  120.  
  121.  
  122.  Boolean System7isUp (void)    {
  123.  
  124.          OSErr        itDoesMatter;
  125.  
  126.  
  127.     itDoesMatter = SysEnvirons(SysEnvironsVersion, &theWorld);
  128.     if ((itDoesMatter == noErr) && (theWorld.systemVersion >= 0x0700))    return (true);
  129.     else    return (false);
  130.  
  131. }    /* System7isUp */
  132.  
  133.  
  134.  
  135. void    System7Braggart (void)    {
  136.  
  137.         short        kSystem7 = 777;
  138.         long        onHeap   = -1;
  139.         SysEnvRec    theWorld;
  140.         OSErr        itDoesMatter;
  141.         GrafPtr        oldPort;
  142.         WindowPtr    window;
  143.         PicHandle    myPic;
  144.         Rect        wpRect, picRect;
  145.         long        finalTicks;
  146.  
  147.  
  148.     itDoesMatter = SysEnvirons(SysEnvironsVersion, &theWorld);
  149.     if ((itDoesMatter == noErr) && (theWorld.systemVersion >= 0x0700))
  150.     {
  151.         GetPort(&oldPort);
  152.  
  153.         window = GetNewWindow(kSystem7, nil, (WindowPtr)onHeap);
  154.         if (window != nil)
  155.         {
  156.             SetPort(window);
  157.             wpRect = window->portRect;
  158.             ClipRect(&wpRect);
  159.             myPic = GetPicture(kSystem7);
  160.  
  161.             if (myPic != nil)
  162.             {
  163.                 picRect = wpRect;
  164.                 InsetRect(&picRect, 1, 1);
  165.                 DrawPicture(myPic, &picRect);    //  ... so it's centered.
  166.                 Delay(60, &finalTicks);
  167.                 InvertRect(&wpRect);
  168.                 Delay(120, &finalTicks);
  169.                 KillPicture(myPic);
  170.             }    /* Got a picture */
  171.  
  172.             DisposeWindow(window);
  173.         }    /* Got a window */
  174.  
  175.         SetPort(oldPort);
  176.  
  177.     }    /* Got System 7 */
  178.  
  179. }    /* System7Braggart */
  180.  
  181.  
  182.  
  183. /* ===================
  184.    A short-cut or two:
  185.    =================== */
  186.  
  187. void LocalGlobal (Rect *r)    {
  188.  
  189.     LocalToGlobal(&topLeft(*r));
  190.     LocalToGlobal(&botRight(*r));
  191.  
  192. }    /* LocalGlobal */
  193.  
  194.  
  195.  
  196. void GlobalLocal (Rect *r)    {
  197.  
  198.     GlobalToLocal(&topLeft(*r));
  199.     GlobalToLocal(&botRight(*r));
  200.  
  201. }    /* GlobalLocal */
  202.  
  203.  
  204.  
  205. short    Max (short a, short b)    {
  206.  
  207.     if (a >= b)    return(a);
  208.     else    return(b);
  209.  
  210. }    /* Max */
  211.  
  212.  
  213.  
  214. short    Min (short a, short b)    {
  215.  
  216.     if (a <= b)    return(a);
  217.     else    return(b);
  218.  
  219. }    /* Min */
  220.  
  221.  
  222.  
  223. double    xPOWERy (double x, double y)    {
  224.  
  225.         double    times, temp;
  226.         
  227.  
  228.     temp = x;
  229.     for (times = y-1; times; times--)    x *= temp;
  230.     return (x);
  231.  
  232. }    /* xPOWERy */
  233.  
  234.  
  235.  
  236. /* ====================================
  237.    Common to the routines that follow:
  238.    Reference: IM, Volume VI, Chapter 3:
  239.    ==================================== */
  240.  
  241.  TrapType GetTrapType (short theTrap)    {
  242.  
  243.         short TrapMask = 0x0800;               /* Tests Bit #11. */
  244.  
  245.  
  246.     if    (theTrap & TrapMask)    return(ToolTrap);
  247.     else    return(OSTrap);
  248.  
  249. }    /* GetTrapType */
  250.  
  251.  
  252.  
  253.  short GetTrapNum (short theTrap)    {
  254.  
  255.         short ToolMask = 0x01FF;
  256.         short OSMask   = 0x00FF;
  257.  
  258.  
  259.     if (GetTrapType(theTrap) == ToolTrap)    return(theTrap & ToolMask);
  260.     else    return(theTrap & OSMask);
  261.  
  262.  }    /* GetTrapNum */
  263.  
  264.  
  265.  
  266.  short NumToolboxTraps (void)    {
  267.  
  268.      /* #define    _InitGraf = 0xA86E; */
  269.         short _Magic    = 0xAA6E;
  270.  
  271.  
  272.     if (NGetTrapAddress(GetTrapNum(_InitGraf), GetTrapType(_InitGraf)) == 
  273.         NGetTrapAddress(GetTrapNum(_Magic), GetTrapType(_Magic)))    return(0x0200);
  274.     else    return(0x0400);
  275.  
  276.  }    /* NumToolboxTraps */
  277.  
  278.  
  279.  
  280.  Boolean TrapAvailable (short theTrap)    {
  281.  
  282.         short        trapNum, tempINT;
  283.         TrapType    tType;
  284.  
  285.  
  286.     trapNum = GetTrapNum(theTrap);
  287.     tType = GetTrapType(theTrap);
  288.  
  289.     if (tType == ToolTrap)    {
  290.         tempINT = theTrap ^ 0xA800;
  291.         if (tempINT >= NumToolboxTraps())    trapNum = GetTrapNum(_Unimplemented);
  292.     }    /* a ToolTrap */
  293.  
  294.     return(NGetTrapAddress(trapNum, tType) != 
  295.            NGetTrapAddress(GetTrapNum(_Unimplemented), GetTrapType(_Unimplemented)));
  296.  
  297. }    /* TrapAvailable */
  298.  
  299.  
  300.  
  301. /* ==============================================
  302.    Now, let's put this new fangled stuff to work:
  303.    ============================================== */
  304.  
  305.  Boolean WNEisImplemented (void)    {
  306.  
  307.     return(TrapAvailable(_WaitNextEvent));
  308.  
  309. }    /* WNEisImplemented */
  310.  
  311.  
  312.  
  313. /* ===============
  314.    Play it, Sam !! 
  315.    =============== */
  316.  
  317.  void PlaySound (char *mySound)    {
  318.  
  319.         Handle            sndHandle;
  320.         OSErr            discardError;
  321.         unsigned char    *pSound;
  322.  
  323.  
  324.     if (TrapAvailable(_SndPlay))
  325.     {
  326.         if (!isPString(mySound))    pSound = CtoPstr(mySound);
  327.         else                        pSound = (unsigned char*)mySound;
  328.  
  329.         sndHandle = GetNamedResource('snd ', pSound);
  330.         if (sndHandle)
  331.         {
  332.             discardError = SndPlay (nil, sndHandle, false);
  333.             ReleaseResource(sndHandle);
  334.         }
  335.     }    /* _SndPlay is implemented */
  336.  
  337. }    /* PlaySound */
  338.  
  339.  
  340.  
  341. long    GetStripAddressMask (void)    {
  342. /* Adapted from Macintosh Tech Note #213 */
  343.  
  344.         long    gLo3Bytes        = 0x00FFFFFF;
  345.      /* #define    _StripAddress = 0xA055; */
  346.  
  347.         long    localBiggee;
  348.  
  349.  
  350.     if (TrapAvailable(_StripAddress))
  351.     {
  352.         localBiggee = 0xFFFFFFFF;
  353.         return((long)StripAddress((Ptr)&localBiggee));
  354.     }
  355.     else    return(gLo3Bytes);
  356.  
  357. }    /* GetStripAddressMask */
  358.  
  359.  
  360.  
  361. Ptr    QuickStrip (long *myPtr)    {
  362.  
  363.     return((Ptr)((long)myPtr & gStripAddressMask));
  364.  
  365. }    /* QuickStrip */
  366.  
  367.  
  368.  
  369. Boolean    isPString (char *myString)    {
  370. /*
  371. ** If FALSE, then it's a C string.
  372. **
  373. ** NOT a totally general approach since we assume that
  374. ** every character in the actual string is printable:
  375. */
  376.  
  377.         short            len, maxLen = 255;
  378.         register short    i;
  379.         Boolean            result, PString = true, CString = false;
  380.         
  381.         
  382.     len = *myString;
  383.     
  384.     if ( !isprint(len) )    result = PString;
  385.     else
  386.     {
  387.         for (i = 1; i <= len; ++i)
  388.             if ( !isprint(myString[i]) )    break;
  389.         /*
  390.         ** If every character is printable, break caused by the
  391.         ** only remaining unprintable character, namely, the C string's
  392.         ** NULL terminator.  If no break happens, i = len + 1:
  393.         */
  394.         if (i <= len)    result = CString;
  395.         else
  396.         {
  397.             for (; i <= maxLen; ++i)
  398.                 if ( !isprint(myString[i]) )    break;
  399.             result = (i > maxLen);        // true:  Pascal string with length = len.
  400.                                         // false: C string with its NULL terminator.
  401.         }
  402.     }
  403.         
  404.     return    (result);
  405.  
  406. }    /* isPString */
  407.  
  408.  
  409.  
  410. void    pStrCat (Str255 s1, Str255 s2, Str255 s3)    {
  411.  
  412.         short    len1, len2;
  413.         
  414.         
  415.     len1 = *s1++;
  416.     len2 = *s2++;
  417.     
  418.     *s3++ = len1 + len2;
  419.     while (--len1 >= 0)    *s3++ = *s1++;
  420.     while (--len2 >= 0)    *s3++ = *s2++;
  421.     
  422. }    /* pStrCat */
  423.  
  424.  
  425.  
  426. void    pStrCopy (Str255 s1, Str255 s2)    {
  427.  
  428.         short    len;
  429.         
  430.         
  431.     len = *s2++ = *s1++;
  432.     /*
  433.         len = *s1++;
  434.         *s2++ = len;
  435.     */
  436.     
  437.     while (--len >= 0)    *s2++ = *s1++;
  438.  
  439. }    /* pStrCopy */
  440.  
  441.  
  442.  
  443. void    DoSpiffyIris (void)        {
  444.  
  445.         short                bragWindowID = 999, IACS = 3001;
  446.         WindowPtr            bragging;
  447.         PicHandle            logoPicHdl, centeredPic;
  448.         short                tempX, tempY, maskPercent;
  449.         Rect                windRect, pictRect, bragRect, irisRect;
  450.         RgnHandle            irisRgn;
  451.         OffScreenRecHdl        bragOSHdl;
  452.         long                finalTicks;
  453.  
  454.  
  455.     SetCursor( *GetCursor(watchCursor) );
  456.  
  457.     if (!gMac2)        bragging = GetNewWindow  (bragWindowID, nil, (WindowPtr)-1);
  458.     else            bragging = GetNewCWindow (bragWindowID, nil, (WindowPtr)-1);
  459.     if (bragging == nil)    {
  460.         SysBeep(10);
  461.         return;
  462.     }
  463.     
  464.     SetPort(bragging);
  465.     TShowWindow(bragging);
  466.     ;
  467.     logoPicHdl = GetPicture(IACS);
  468.     if (logoPicHdl == nil)    {
  469.         SysBeep(10);
  470.         return;
  471.     }
  472.     
  473.     bragRect = bragging->portRect;
  474.  
  475.  /* if (ScrollVert(bragging) != nil)
  476.         bragRect.right = (bragRect.right + frame) - scrollWidth;
  477.     if (ScrollHoriz(bragging) != nil)
  478.         bragRect.bottom = (bragRect.bottom + frame) - scrollHeight; */
  479.         
  480.     pictRect = (**logoPicHdl).picFrame;        // Center the PICT ...
  481.     CenterRects(&pictRect, &bragRect);
  482.     
  483.     // Make a new PICT, this one centered & clipped to my portRect:
  484.     ClipRect(&bragRect);
  485.     centeredPic = OpenPicture(&bragRect);
  486.     ClipRect(&bragRect);
  487.     EraseRect(&bragRect);
  488.     InvertRect(&bragRect);
  489.     DrawPicture(logoPicHdl, &pictRect);
  490.     ClosePicture();
  491.     ;
  492.     MoveHHi(centeredPic);
  493.     HLock(centeredPic);
  494.     ReleaseResource(logoPicHdl);        /* Kiss the original good-bye !! */
  495.     
  496.     /* -------------------------------------------
  497.     ** Place my PICTure into an off screen BitMap.
  498.     ** ------------------------------------------- */
  499.  
  500.     /* Local window in, local screen back: */
  501.     bragOSHdl = CreateOffScreen(&bragRect);
  502.     if ( ((*bragOSHdl)->CreateOffScreenError) != noErr)
  503.         goto byebye;
  504.     {
  505.         ClipRect(&bragRect);                        /* Draw off-screen ... */
  506.         EraseRect(&bragRect);                        /* Eliminate all stray matter. */
  507.         DrawPicture(centeredPic, &bragRect);
  508.     }
  509.     ToOnScreen(bragOSHdl);                            /* Back to "Square 1". */
  510.     
  511.     windRect = (**bragOSHdl).drawingRect;
  512.     ClipRect(&windRect);
  513.  // InvertRect(&windRect);                            /* Part of centeredPic. */
  514.  
  515.     BackColor(whiteColor);
  516.     ForeColor(blackColor);
  517.     /* ---------- */
  518.     maskPercent = 99;                                /* Initialize some stuff ... */
  519.     tempX = (windRect.right - windRect.left) / 2;
  520.     tempY = (windRect.bottom - windRect.top) / 2;
  521.     ;
  522.     while (maskPercent >= 0)    {
  523.  
  524.         irisRect = windRect;
  525.         InsetRect(&irisRect,
  526.                   (tempX * maskPercent) / 100,
  527.                   (tempY * maskPercent) / 100);
  528.         irisRgn = NewRgn();
  529.         OpenRgn();
  530.         {
  531.             FrameRoundRect(&irisRect,
  532.                            ((irisRect.right - irisRect.left) * maskPercent) / 100,         
  533.                            ((irisRect.bottom - irisRect.top) * maskPercent) / 100);
  534.         }
  535.         CloseRgn(irisRgn);
  536.  
  537.         SectRgn(irisRgn, ((**bragOSHdl).origPort)->visRgn, irisRgn);
  538.         CopyBits(
  539.                     (**bragOSHdl).offBitMapPtr,
  540.                     &( ((**bragOSHdl).origPort)->portBits ),
  541.                     &bragRect,
  542.                     &( (**bragOSHdl).drawingRect ),
  543.                     srcCopy,
  544.                     irisRgn
  545.                 );
  546.  
  547.         DisposeRgn(irisRgn);
  548.         ;
  549.         if (gMac2)    {
  550.             if (gColorDepth = 1)
  551.                 Delay(4, &finalTicks);        /* Black-and-white too doggone fast !! */
  552.             else
  553.                 Delay(2, &finalTicks);        /* Color a tad better. */
  554.         }
  555.         else
  556.             Delay(1, &finalTicks);
  557.         ;
  558.         maskPercent = maskPercent - 1;
  559.     }    /* while maskPercent >= 0 */
  560.     /* ---------- */
  561.  
  562. byebye:
  563.     DisposOffScreen(&bragOSHdl);
  564.  
  565.      SetWindowPic(bragging, centeredPic);    /* ... for updating time. */
  566.  
  567.  //    DisposeWindow(bragging);                /* Leave it be !! */
  568.  
  569.     SysBeep(10);                            /* Wake Up Call !! */
  570.  
  571. }    /* DoSpiffyIris */
  572.  
  573.  
  574.  
  575. void    CenterRects (Rect *srcRect, Rect *withinRect)    {
  576.  
  577.         short        srcW, srcH, tempX, tempY;
  578.  
  579.     
  580.     srcW = srcRect->right - srcRect->left;
  581.     // within width - source width:
  582.     tempX = withinRect->right - withinRect->left - srcW;
  583.     srcRect->left = withinRect->left + tempX/2;
  584.     srcRect->right = srcRect->left + srcW;
  585.     ;
  586.     srcH = srcRect->bottom - srcRect->top;
  587.     // within height - source height:
  588.     tempY = withinRect->bottom - withinRect->top - srcH;                            
  589.     srcRect->top = withinRect->top + tempY/2;
  590.     srcRect->bottom = srcRect->top + srcH;
  591.     
  592. }    /* CenterRects */
  593.  
  594.  
  595.  
  596. Boolean        cmpAlpha (char in, char with)    {
  597. /* If the input is NOT a letter, then we advance to the NEXT available letter. */
  598.  
  599.         Boolean        result = false;
  600.         
  601.         
  602.     if ( !isalpha(with) )    SysBeep(10);
  603.     else    {
  604.     
  605.         if ( !isalpha(in) )        in = nextAlpha(in);
  606.         ;
  607.         if ( isupper(with) && islower(in) )
  608.             in = toupper(in);
  609.         else if ( islower(with) && isupper(in) )
  610.             in = tolower(in);
  611.         ;
  612.         if (in == with)        result = true;
  613.             
  614.     }    /* end: with isalpha */
  615.     
  616.     return (result);
  617.     
  618. }    /* cmpAlpha */
  619.  
  620.  
  621.  
  622. char    nextAlpha (char start)        {
  623.  
  624.         char    next;
  625.         
  626.         
  627.     next = (char) (start + 1);
  628.     
  629.     if ( !isalpha(next) )    {
  630.         if ((next < 'A') || (next > 'z'))    next = 'A';
  631.         else    /* inbetween */                next = 'a';
  632.     }
  633.     
  634.     return (next);
  635.     
  636. }    /* nextAlpha */
  637.  
  638.  
  639.  
  640.  
  641. /*    { end file = "misc.c" }  */